package com.msti.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.msti.entity.Batch;
import com.msti.entity.Course;
import com.msti.entity.Student;
import com.msti.sessionMgt.ApplicationSession;

public class BatchMgtDAOImpl implements BatchMgtDAO{

	@Override
	public boolean createNewBatch(Batch batch) {
		Session session = ApplicationSession.getSession();
		try {
			session.beginTransaction();
			session.save(batch);
			session.getTransaction().commit();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
		}
		finally{
			session.close();
		}
		return true;
	}

	@Override
	public boolean deleteBatch(Batch batch) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean editBatch(Batch batch) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<Batch> getAllBatch() {
		Session session = ApplicationSession.getSession();
		List<Batch> batchs = new ArrayList<Batch>();
		try {
			long eId = 0;
			Query query = session.createQuery("from Batch");
			batchs = (List<Batch>) query.list();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
			System.out.println(he);
		}
		finally{
			session.close();
		}
		return batchs;
	}

	@Override
	public Batch getBatchByCourse(Course c) {
		Session session = ApplicationSession.getSession();
		Batch batch = null;
		try {
			Query query = session.createQuery("from Batch where course = ?")
					.setParameter(0, c);
			batch = (Batch)query.uniqueResult();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
			System.out.println(he);
		}
		finally{
			session.close();
		}
		return batch;
	}

	@Override
	public void update(Batch batch) {
		Session session = ApplicationSession.getSession();
		try {
			session.beginTransaction();
			session.update(batch);
			session.getTransaction().commit();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
		}
		finally{
			session.close();
		}
		
	}


}
